﻿ng.module('smart-table')
.directive('stSelectRow', ['stConfig', function (stConfig) {
    return {
        restrict: 'A',
        require: '^stTable',
        scope: {
            row: '=stSelectRow'
        },
        link: function (scope, element, attr, ctrl) {
            var mode = attr.stSelectMode || stConfig.select.mode;
            element.bind('click', function () {
                scope.$apply(function () {
                    ctrl.select(scope.row, mode);
                });
            });

            scope.$watch('row.isSelected', function (newValue) {
                if (newValue === true) {
                    element.addClass(stConfig.select.selectedClass);
                } else {
                    element.removeClass(stConfig.select.selectedClass);
                }
            });
        }
    };
}])
.directive('editableText', function () {
    return {
        restrict: 'E',
        require: 'ngModel',
        scope: {
            ngModel: "=ngModel",
        },
        template: "<span class='editable-column' uib-popover-template='dynamicPopover.templateUrl' popover-is-open='isOpen'>{{ngModel || 'edit'}}</span>",   //ng-class='{'editable-empty': !ngModel.length}'
        replace: true,
        link: function (scope, element, attrs, ngModel) {

            scope.dynamicPopover = {
                templateUrl: 'templates/EditableTextField.html',
                ngModel: scope.ngModel,
                update: function () {
                    scope.ngModel = scope.dynamicPopover.ngModel;
                    scope.$parent.quoteCtrl.changeLines(scope.dynamicPopover.ngModel, attrs.name, scope.$parent.$index);
                    scope.isOpen = false;
                },
                close: function () {
                    scope.isOpen = false;
                }
            };
        }
    }
});
